home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / HD-Installer / jst_dev / sources / src / DiskTools / rob2file.c < prev    next >
C/C++ Source or Header  |  2000-04-12  |  4KB  |  204 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4.  
  5. #include <dos/dos.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <exec/execbase.h>
  9. #include <exec/libraries.h>
  10. #include <exec/memory.h>
  11. #include <pragmas/dos_pragmas.h>
  12. #include <string.h>
  13.  
  14. #define RAWTRACK_LEN 0x8000
  15. #define TRACK_LEN 0x1800
  16. #define NB_RETRIES 10
  17.  
  18. extern struct ExecBase *SysBase;
  19.  
  20. extern APTR InitTrackDisk(ULONG);
  21. extern void ShutTrackDisk(void);
  22. extern ULONG CheckDiskIn (void);
  23.  
  24. extern ULONG ReadRawTrackIndex(ULONG,UBYTE *);
  25. extern ULONG DecodeTrack(UBYTE *, UBYTE *, ULONG);
  26. UBYTE *rawTrackBuffer=0L;
  27. UBYTE *decTrackBuffer=0L;
  28. char *charunit="DF0:";
  29. int allocated=0,diskinit=0;
  30. FILE *f;
  31.  
  32. void CloseAll(char *mess)
  33.  
  34. {
  35. if (f) fclose(f);
  36.  
  37. if (mess) printf("** %s\n",mess);
  38.  
  39. if (diskinit) ShutTrackDisk();
  40. if (allocated) Inhibit(charunit,FALSE);
  41. if (rawTrackBuffer) FreeMem((APTR)rawTrackBuffer,RAWTRACK_LEN);
  42. if (decTrackBuffer) FreeMem((APTR)decTrackBuffer,TRACK_LEN);
  43.  
  44.  
  45. exit(0);
  46. }
  47.  
  48. void BreakHandle(int code)
  49.  
  50. {
  51. CloseAll("User break");
  52. }
  53.  
  54.  
  55. int findArg(unsigned int argc,char ** argv,char * argstr)
  56. {
  57.   int argl,i;
  58.  
  59.   argl=strlen(argstr);
  60.  
  61.   for (i=0;i<argc;i++)
  62.     if (!strncmp(argv[i],argstr,argl)) return i;
  63.  
  64.   return 0;
  65. }
  66.  
  67.  
  68. main(unsigned int argc,char ** argv)
  69.  
  70.  
  71. {
  72.   ULONG diskunit=0,starttrack=2,endtrack=159; /* default values */
  73.   ULONG diskkey=0,offset=0;
  74.   int i,retries,readok,dcrslt;
  75.   char filename[108]="";
  76.  
  77.   signal(SIGINT,BreakHandle); /* CTRL-C caught */
  78.  
  79.   printf("\2331;37;40mRob2File V2.0 \2330;31;40m - The Rob Northen disk image maker by JF Fabre\n");
  80.  
  81.   switch(argc)
  82.  
  83.      {
  84.     case 7:
  85.         if (!stricmp("OFFSET",argv[6])) offset=1;
  86.     case 6:
  87.         sscanf(argv[5],"%x",&diskkey);
  88.     case 5:
  89.         endtrack=atoi(argv[4]);
  90.         if ((endtrack>159)||(endtrack<0))
  91.               CloseAll("StartTrack must be between 0 and 159\n");
  92.     case 4:
  93.         starttrack=atoi(argv[3]);
  94.         if ((starttrack>endtrack)||(starttrack<0))
  95.               CloseAll("StartTrack must be between 0 and 159\n");
  96.  
  97.     case 3:
  98.         strcpy(filename,argv[2]);
  99.         diskunit=(ULONG)argv[1][0]-'0';
  100.         break;
  101.         
  102.     default:
  103.         printf("\nUsage : rob2file <unit> <filename> [<starttrack>] [<endtrack>] [<diskkey>] [OFFSET]\n");
  104.         printf("Defaults are:\n  StartTrack: 2\n  EndTrack: 159\n  Diskkey: 0"
  105.                 "\n\nYou must specify a filename for the destination\n");
  106.         if (!argc) Delay(200);
  107.         CloseAll(0);
  108.     }
  109.  
  110.  
  111.   diskunit=(ULONG)argv[1][0]-'0';
  112.  
  113.   if ((diskunit>3)||(diskunit<0)) {printf("** Unit %d unavailable\n",diskunit);CloseAll(0);}
  114.  
  115.   charunit[2]=diskunit+'0';
  116.  
  117.   if (SysBase->LibNode.lib_Version>36)
  118.     {
  119.     if(Inhibit(charunit,DOSTRUE)==FALSE) CloseAll("Can't allocate device !");
  120.     allocated=1;
  121.     }
  122.  
  123.   if ((ULONG)InitTrackDisk(diskunit)<0) {printf("** Can't open unit %d !",diskunit);CloseAll(0);}
  124.   diskinit=1;
  125.   if (CheckDiskIn()) {printf("** No disk in unit %d !\n",diskunit);CloseAll(NULL);}
  126.  
  127.   decTrackBuffer=(char *)AllocMem(TRACK_LEN,MEMF_CLEAR);
  128.   if (decTrackBuffer==0L) CloseAll("Can't allocate memory.");
  129.  
  130.   rawTrackBuffer=(char *)AllocMem(RAWTRACK_LEN,MEMF_CHIP|MEMF_CLEAR);
  131.   if (rawTrackBuffer==0L) CloseAll("Can't allocate chip memory.");
  132.  
  133.   f=fopen(filename,"wb");
  134.  
  135.   if (f==NULL) CloseAll("Can't create destination file!");
  136.  
  137.   printf("Reading disk from track %d to track %d (included)\n",starttrack,endtrack);
  138.  
  139.   if (offset)
  140.   {
  141.     printf("Adding offset\n");
  142.  
  143.     for (i=0;i<starttrack;i++)
  144.     {
  145.       if (fwrite(decTrackBuffer,1,TRACK_LEN,f)!=TRACK_LEN)
  146.       {
  147.          CloseAll("Disk full !");
  148.       }
  149.    }
  150.   }
  151.  
  152.   for (i=starttrack;i<endtrack+1;i++)
  153.     {
  154.     retries=0;
  155.     readok=0;
  156.  
  157.     do
  158.     {
  159.  
  160.         if (ReadRawTrackIndex(i,rawTrackBuffer))
  161.             {
  162.             if (retries>NB_RETRIES) CloseAll("Low level disk read error!");
  163.             }
  164.  
  165.         else
  166.         
  167.             {    
  168.  
  169.              dcrslt=DecodeTrack(rawTrackBuffer,decTrackBuffer,diskkey);
  170.  
  171.             if (!dcrslt)
  172.                     {
  173.                         readok=1;
  174.                         if (fwrite(decTrackBuffer,1,TRACK_LEN,f)!=TRACK_LEN) CloseAll("Disk full !");
  175.                     }
  176.  
  177.             else
  178.                     {
  179.                         if (retries>NB_RETRIES)
  180.                         {
  181.                             printf("** Error Track %d",i);
  182.  
  183.                             switch(dcrslt)
  184.                             {    
  185.                             case -1: printf("\n");CloseAll("Decode error. Not a RN disk!");
  186.                             case -2: printf("\n");CloseAll("No sync found!");
  187.                             default: printf(" Sector %d\n",dcrslt);CloseAll("Decode error. Wrong key or checksum error!");
  188.                             }
  189.                         }
  190.                     }
  191.  
  192.             }
  193.  
  194.     retries++;
  195.     }
  196.     while (!readok);
  197.     }
  198.  
  199.     printf("Disk image done\n");
  200.  
  201.     CloseAll(0);
  202. }
  203.  
  204.